//
//  MessageViewController.m
//  LOLBOX
//
//  Created by lanou3g on 15/12/17.
//  Copyright © 2015年 张明. All rights reserved.
//

#import "MessageViewController.h"
#import "MessageNextViewController.h"
#import "InformationHelp.h"
#import "TopView.h"
#import "Information.h"
#import "NewTableViewCell.h"

#define inforURLNew @"http://qt.qq.com/static/pages/news/phone/c12_list_1.shtml"

#define inforURLOff @"http://qt.qq.com/static/pages/news/phone/c3_list_1.shtml"

#define inforURLCom @"http://qt.qq.com/static/pages/news/phone/c73_list_1.shtml"

#define inforURLBel @"http://qt.qq.com/static/pages/news/phone/c17_list_1.shtml"

@interface MessageViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)TopView *TopView;
@property(nonatomic ,strong)UITableView *NewTableView;
@property(nonatomic,strong)Information *Newmodel;
@property(nonatomic ,strong)NSMutableArray *allDataArray;
@property(nonatomic ,strong)InformationHelp *inforHelp;

@end

@implementation MessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.Newmodel = [[Information alloc]init];
    
    self.NewTableView = [[UITableView alloc]initWithFrame:CGRectMake(0,0,[UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) style:UITableViewStyleGrouped];

    [self.NewTableView registerNib:[UINib nibWithNibName:@"NewTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    
    self.NewTableView.delegate = self;
    self.NewTableView.dataSource = self;
    
    [self.view addSubview:self.NewTableView];
    [self setUpTopView];
    [self setupNav];
    
    self.inforHelp = [InformationHelp shareWithHelpData];
    [self.inforHelp getInforListDataWithUrl:inforURLNew successData:^(NSMutableArray *array) {
        self.allDataArray = self.inforHelp.allDataMutableArr;
       [self.NewTableView reloadData];
    } fail:^(NSError *err) {
        
    }];
}


-(void)setupNav
{
    // 设置导航栏上面的内容
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-message"] style:UIBarButtonItemStyleDone target:self action:@selector(btnLeftAciton)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"icon_search"] style:UIBarButtonItemStyleDone target:self action:@selector(btnrightAciton)];
}

//btnLeftAciton
-(void)btnLeftAciton
{
    NSLog(@"btnLeftAciton");
}

-(void)btnrightAciton
{
    NSLog(@"btnrightAciton");
}

-(void)setUpTopView
{
    self.TopView = [[TopView alloc]init];
    
    [self.TopView.seg addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:self.TopView];
}

// TopButtonClick 点击事件 更换 tableView
-(void)segAction:(UISegmentedControl *)sender
{
    if (sender.selectedSegmentIndex == 0) {
        
        [self.inforHelp getInforListDataWithUrl:inforURLNew successData:^(NSMutableArray *array) {
            self.allDataArray = self.inforHelp.allDataMutableArr;
            [self.NewTableView reloadData];
        } fail:^(NSError *err) {
            
        }];
        
        NSLog(@"点击新闻0");
    }else if(sender.selectedSegmentIndex == 1){
        
        [self.inforHelp getInforListDataWithUrl:inforURLOff successData:^(NSMutableArray *array) {
            self.allDataArray = self.inforHelp.allDataMutableArr;
            [self.NewTableView reloadData];
        } fail:^(NSError *err) {
            
        }];
        NSLog(@"点击新闻1");
    }else if(sender.selectedSegmentIndex == 2){
        [self.inforHelp getInforListDataWithUrl:inforURLCom successData:^(NSMutableArray *array) {
            self.allDataArray = self.inforHelp.allDataMutableArr;
            [self.NewTableView reloadData];
        } fail:^(NSError *err) {
            
        }];
        NSLog(@"点击新闻2");
    }else{
        
        [self.inforHelp getInforListDataWithUrl:inforURLBel successData:^(NSMutableArray *array) {
            self.allDataArray = self.inforHelp.allDataMutableArr;
            [self.NewTableView reloadData];
        } fail:^(NSError *err) {
            
        }];
        NSLog(@"点击新闻3");
    }
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 20;
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    self.Newmodel = self.allDataArray[indexPath.row];
    cell.titleLabel.text = self.Newmodel.title;
    cell.titleLabel.numberOfLines = 0;
    cell.contentLabel.text = self.Newmodel.summary;
    cell.timeLabel.text = [NSString stringWithFormat:@"发表时间:%@",self.Newmodel.publication_date];
    [cell.image sd_setImageWithURL:[NSURL URLWithString:self.Newmodel.image_url_small] placeholderImage:[UIImage imageNamed:@"1.png"]];
    
    return cell;
}


/** 点击cell 触发事件 */
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MessageNextViewController *messageVC = [[MessageNextViewController alloc] init];
    
    Information *model = self.allDataArray[indexPath.row];
    
    //    if (indexPath.row > 0) {
    if ([model.article_url hasPrefix:@"http://"]) {
        messageVC.urlStr = model.article_url;
        
    }else {
        NSString *str = [NSString stringWithFormat:@"http://qt.qq.com/static/pages/news/phone/%@?APP_BROWSER_VERSION_CODE=1&ios_version=665&imgmode=auto", model.article_url];
        messageVC.urlStr = str;
    }
    //    }
    [self.navigationController pushViewController:messageVC animated:YES];
//    [self showViewController:messageNC sender:self];

}

#pragma mark 设置每行高度（每行高度可以不一样）
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        UIView *view = [[UIView alloc]init];
        view.backgroundColor = [UIColor greenColor];
        return view;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (self.TopView.seg.selectedSegmentIndex == 0) {
        return 200;
    }
    return 0;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
